package com.chujian.Adapter;

import java.util.List;
import java.util.Map;

import za.co.immedia.pinnedheaderlistview.SectionedBaseAdapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chujian.Global.Global;
import com.chujian.Utils.ImageLoader;
import com.chujian.model.Food;
import com.chujian.orderingsystem.R;
/**
 * 点餐模块的适配器，linkedlistview
 * @author 周映凯
 *
 */
public class MapSectionAdapter extends SectionedBaseAdapter {
	private Context mContext;
	private Button btnYiXuan;
	private List<String> leftStrList;
	Map<String , List<Food>> rightStrMap;
//	Map<Food, Integer>selectedFood;
	ImageLoader mImageLoader;
	public MapSectionAdapter(Context mContext, List<String> leftStrList,
			Map<String, List<Food>> rightStrMap,Button btnYiXuan) {
		super();
		this.mContext = mContext;
		this.leftStrList = leftStrList;
		this.rightStrMap = rightStrMap;
		this.btnYiXuan=btnYiXuan;
		mImageLoader=new ImageLoader();
		
		
	}

	@Override
	public Object getItem(int section, int position) {
		// TODO Auto-generated method stub
		return rightStrMap.get(leftStrList.get(section)).get(position);
	}

	@Override
	public long getItemId(int section, int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getSectionCount() {
		// TODO Auto-generated method stub
		if(leftStrList==null)
			return 0;
		
		return leftStrList.size();
	}

	@Override
	public int getCountForSection(int section) {
		// TODO Auto-generated method stub
		return rightStrMap.get(leftStrList.get(section)).size();
	}

	@Override
	public View getItemView(final int section, final int position, View convertView,
			ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder=null;
		if(convertView==null){
			LayoutInflater inflator=(LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView =  inflator.inflate(R.layout.linkedlist_item, null);
			viewHolder=new ViewHolder();
			viewHolder.ivIcon=(ImageView) convertView.findViewById(R.id.iv_food);
			viewHolder.tvFoodName=(TextView) convertView.findViewById(R.id.tv_food_name);
			viewHolder.tvSeldCount=(TextView) convertView.findViewById(R.id.tv_sell_count);
			viewHolder.tvZanCount=(TextView) convertView.findViewById(R.id.tv_zan_count);
			viewHolder.tvPrice=(TextView) convertView.findViewById(R.id.tv_price);
			viewHolder.tvUnit=(TextView) convertView.findViewById(R.id.tv_unit);
			viewHolder.btnAddOne=(Button) convertView.findViewById(R.id.btn_add_one);
			convertView.setTag(viewHolder);
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
		viewHolder.btnAddOne.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(mContext, (rightStrMap.get(leftStrList.get(section)).get(position).getFoodName()), Toast.LENGTH_SHORT).show();
			}
		});
		
		final Food food=rightStrMap.get(leftStrList.get(section)).get(position);
//		默认图片
		viewHolder.ivIcon.setImageResource(R.drawable.loading);
//		初始化数据
		String url=rightStrMap.get(leftStrList.get(section)).get(position).getFoodUrl();
		viewHolder.ivIcon.setTag(url);//把图片地址与url绑定
		if(!url.equals("")){
			mImageLoader.showImageByAsyncTask(viewHolder.ivIcon,url);
		}
		viewHolder.tvFoodName.setText(rightStrMap.get(leftStrList.get(section)).get(position).getFoodName());
		viewHolder.tvSeldCount.setText("销量"+rightStrMap.get(leftStrList.get(section)).get(position).getSellCount());
		viewHolder.tvZanCount.setText(""+rightStrMap.get(leftStrList.get(section)).get(position).getFoodZan());
		viewHolder.tvPrice.setText(rightStrMap.get(leftStrList.get(section)).get(position).getFoodPrice()+"");
		viewHolder.tvUnit.setText("/"+rightStrMap.get(leftStrList.get(section)).get(position).getFoodUnit());
//		rightlistitem 按钮的监听
		viewHolder.btnAddOne.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				if(Global.selectedFood.containsKey(food)){
				   Integer foodCount=Global.selectedFood.get(food);
				   Global.selectedFood.put(food, ++foodCount);
				}else{
					Global.selectedFood.put(food, 1);
				}
				Global.count++;
				// TODO Auto-generated method stub
//				api level 16
				btnYiXuan.setBackgroundResource(R.drawable.yellow_button_selector);
				btnYiXuan.setClickable(true);
				btnYiXuan.setText("已选菜品（"+Global.count+"）");
			}
		});
		
		
		return convertView;
	}

	class ViewHolder{
		public ImageView ivIcon;
		public TextView tvFoodName;
		public TextView tvSeldCount;
		public TextView tvZanCount;
		public TextView tvPrice;
		public TextView tvUnit;
		public Button btnAddOne;
	}
	@Override
	public View getSectionHeaderView(int section, View convertView,
			ViewGroup parent) {
			LinearLayout layout=null;
			if(convertView==null){
				LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				layout=(LinearLayout) inflator.inflate(R.layout.header_item, null);
			}else{
				layout=(LinearLayout) convertView;
			}
			layout.setClickable(false);
			layout.setBackgroundColor(Color.rgb(248, 248, 248));
			TextView tv=(TextView) layout.findViewById(R.id.textItem);
			tv.setText(leftStrList.get(section));
		return layout;
	}

}
